// src/main.ts

import { HttpAdapterHost, NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common';
import { PrismaClientExceptionFilter } from './prisma-client-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // whitelist: true 开启这个，前端传输的参数，在dto里面没有定义装饰器的参数会被去掉，即使dto里有对应的属性
  app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
  // ClassSerializerInterceptor 使用 class-transformer 包来定义如何转换对象。使用 @Exclude（） 装饰器排除 UserEntity 类中的密码字段
  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));

  const config = new DocumentBuilder()
    .setTitle('Median')
    .setDescription('The Median API description')
    .setVersion('0.1')
    .addBearerAuth()
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);

  const { httpAdapter } = app.get(HttpAdapterHost);
  app.useGlobalFilters(new PrismaClientExceptionFilter(httpAdapter));

  // 3000 好像因为docker冲突
  await app.listen(4000);
}

bootstrap();
